#!/usr/bin/perl -w
#读取about_shenji.file文件的信息
     my $SHENJICONF;
     use constant SHENJI_FILE=>'</var/www/html/upload/about_shenji.conf';
     my %shenji_conf = ();
     #从文件读取数据库信息

     open($SHENJICONF, SHENJI_FILE) or die "can't open \n";
     while(my $line = <$SHENJICONF>)
     {
         chomp $line;
         (my $name, my $info) = (split /=/, $line)[0,1];
         $shenji_conf{$name} = $info;
         $shenji_conf{$name} =~ s/\s//g;
     }
	
     close $SHENJICONF;
     my $way=$shenji_conf{"way"};
     my $status=$shenji_conf{"status"};
     my $version=$shenji_conf{"version"};
	 
     my $relversion=$version+0.01;
	 my $jy_filename=$relversion."_update.tar.gz";
	
	 if($status eq 1){#有压缩包需要升级
	      #判断run.sh还在跑
		  my $jc=`ps -ef`;
		  if($jc =~/run.sh/){
		     exit;
		  }
	
	     #删除压缩包update.tar.gz
	     my $sc_xian_update="/var/www/html/upload/shenjibao/update.tar.gz";   
	     if( -e $sc_xian_update){  
	   
             my $ee=`rm -rf $sc_xian_update`;
		   
        }
	
	    #得到压缩包的文件名
	      my $DIR;
		  opendir(DIR,'/var/www/html/upload/shenjibao/');
		  while(my $file = readdir(DIR)){
			  if($file=~/update.tar.gz/){
				 $rel_file=$file;#压缩包的名称
				 $dq_version=$rel_file;
				 $dq_version=~s/_update.tar.gz//g;#版本
			  }     
		  }
		  closedir(DIR);
		
		   my $res=`cd /var/www/html/upload/shenjibao/ && ./cmpmd5ssl.pl $rel_file`;
		   #解密(此处路径为cmpmd5ssl.pl放的路径,可变)得到升级包和update.tar.gz
		
		   #把加密的包删除
		   my $sc_wenjian="/var/www/html/upload/shenjibao/$rel_file";
           if($rel_file ne "" && -e $sc_wenjian){
		        my $ee1=`rm -rf $sc_wenjian`;
		   }
		
		   if($res=~/It's OK/){#解密成功
		     
			   #解压update(解压之前需要删除里面的update文件或删除里面的压缩包)
		         my $filename ="/var/www/html/upload/shenjibao/update/"; 
                 if( -e $filename){  
                  my $ee=`rm -rf $filename`;
                 }
            
			    #解压路径第一次解压路径(解压的结果为update文件夹里面有真正用来升级的压缩包，可变)
                my $info=`cd /var/www/html/upload/shenjibao && tar -zxvf /var/www/html/upload/shenjibao/update.tar.gz`;
			 
			 
			   ##解压之后run.sh放的位置
                my $re=`cd /var/www/html/upload/shenjibao/update/ && chmod 777 run.sh`;
                my $re2=`cd /var/www/html/upload/shenjibao/update/ && ./run.sh`;
			 
			 
			     if($re2 =~'100%'){#升级成功
				   
				      if($dq_version gt $version){#升级
				       
                          open($filevar,">/var/www/html/upload/about_shenji.conf")|| die("Could not open file");
                          my $xie_ru="way=$way\n"."status=0"."\n"."version=$relversion";
                          print $filevar $xie_ru;
                          close $filevar;
                 
                       }else{#更新
     
                           open($filevar,">/var/www/html/upload/about_shenji.conf")|| die("Could not open file");
                           my $xie_ru="way=$way\n"."status=0"."\n"."version=$version";
                           print $filevar $xie_ru;
                           close $filevar;
                 
                       }
					
			 
			    }else{
				
				          open($filevar,">/var/www/html/upload/about_shenji.conf")|| die("Could not open file");
                          my $xie_ru="way=$way\n"."status=2"."\n"."version=$version"."\n"."shenjibao=$rel_file";
                          print $filevar $xie_ru;
                          close $filevar;
				}
		     
	      }else{
		  
		                  open($filevar,">/var/www/html/upload/about_shenji.conf")|| die("Could not open file");
                          my $xie_ru="way=$way\n"."status=2"."\n"."version=$version"."\n"."shenjibao=$rel_file";
                          print $filevar $xie_ru;
                          close $filevar;
		  
		  }

	 }
	
	